<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-09
 * Time: 13:44
 */

namespace app\api\controller;

use app\common\model\GoodsClasses as GoodsclassesModel;
use app\common\model\OrderGoodsclasses;

class Goodsclasses extends Base
{

    public static $goods_base_filed = 'id,name,price,shop_price,picture,picture_list,ord,stock,shop_type';

    //商品详情
    public function detail()
    {
        $goods_id = input('id');
        if (empty($goods_id)) {
            $this->ajax(202, '商品ID不能为空');
        }
        $detail = GoodsclassesModel::getOne(['id' => ['eq', $goods_id]]);
        $detail['picture'] = tomedia($detail['picture']);
        $pic_list = json_decode($detail['picture_list'], true);
        foreach ($pic_list as $pk => $pv) {
            $pic_list[$pk]['path'] = tomedia($pv['path']);
        }
        $detail['picture_list'] = $pic_list;
        $this->ajax(200, 'success', $detail);
    }

    //处理商品
    public function dealGoods($goods)
    {
        foreach ($goods as $k => $v) {
            $goods[$k]['picture'] = tomedia($v['picture']);
            $pic_list = json_decode($v['picture_list'], true);
            foreach ($pic_list as $pk => $pv) {
                $pic_list[$pk]['path'] = tomedia($pv['path']);
            }
            $goods[$k]['picture_list'] = $pic_list;

        }
        return $goods;
    }

    /* 商品列表 */
    public function list()
    {
        $input = input();
        $where = [
            'onsale' => 1
        ];
        $orderby = 'ord desc';
        if ($input['keyword']) {
            $where['name'] = [
                'like', "%{$input['keyword']}%"
            ];
        }
        $page = max($input['page'], 1);
        $size = 20;
        $list = GoodsclassesModel::getPage($where, $page, $size, $orderby);
        foreach ($list as $k => $v) {
            $list[$k]['picture'] = tomedia($v['picture']);
            $pic_list = json_decode($v['picture_list'], true);
            foreach ($pic_list as $pk => $pv) {
                $pic_list[$pk]['path'] = tomedia($pv['path']);
            }
            $list[$k]['picture_list'] = $pic_list;
        }
        if (!$list) {
            $this->ajax(200, 'success', []);
        }
        $this->ajax(200, 'success', $list);
    }

    public function order()
    {
        if (request()->isPost()) {
            //表单信息
            $input = input('');
            //查询课程商品信息
            $goods_where = [
                'id' => ['eq', $input['id']]
            ];
            //课程商品信息
            $goods = GoodsclassesModel::getOne($goods_where);
            $time = time();
            //订单信息
            $save = [
                'mall_id' => $this->mall_id,
                'uid' => $this->uid,
                'title' => $goods['name'],
                'update_time' => $time,
                'create_time' => $time,
                'order_sn' => create_order_sn($this->uid, 'GC'),
                'goods_list' => json_encode($goods),
                'checked' => 0,
                'all_money' => $goods['price'],
            ];
            $id = OrderGoodsclasses::setInsertGetId($save);
            $this->ajax(200, 'success', $id);
        }
    }


}